package com.imooc.retrofit;

import okhttp3.HttpUrl;
import okhttp3.Request;

public class RequestBuilder {
    final ParameterHandler<?>[] parameterHandlers;
    final Object[] args;
    HttpUrl.Builder httpUrl;
    public RequestBuilder(String baseUrl, String relativeUrl, String httpMethod,
                          ParameterHandler<?>[] parameterHandlers, Object[] args) {
        this.parameterHandlers=parameterHandlers;
        this.args = args;
        httpUrl = HttpUrl.parse(baseUrl+relativeUrl).newBuilder();
    }

    public Request build(){

        int count = args.length;
        for (int i = 0; i < count; i++) {
            // userName = ""
            parameterHandlers[i].apply(this,args[i]);
        }

        Request request = new Request.Builder().url(httpUrl.build()).build();
        return request;
    }

    public void addQueryName(String key, String value) {
        // userName = ""&password=""
        httpUrl.addQueryParameter(key,value);
    }
}
